#!/usr/bin/env python
# coding=utf-8
'''
在Debian Security Tracker中（例：https://security-tracker.debian.org/tracker/source-package/clamav）
获取Source Package对应的CVE信息
'''

import requests
from lxml import html as H
from DBUtils import GetSourcePackageList,InsertDstGroundTruth
import re


cve_regex = r'(CVE-\d{4}-\d+)'



for sp, in GetSourcePackageList():
    try:
        text = requests.get("https://security-tracker.debian.org/tracker/source-package/{}".format(sp)).text
    except requests.RequestException:
        text = None
    if not text:
        continue
    dom = H.fromstring(text)
    links = dom.xpath("//a")
    cve_list = []
    for link in links:
        result = re.findall(cve_regex, link.text_content())
        cve_list.extend(result)
    InsertDstGroundTruth(sp, cve_list)

